// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef NET_COMM_SERVER_H
#define NET_COMM_SERVER_H

#include "Include/msghead.h"
#include "Log/logging.h"
#include "Communication/Socket/VmiSocket.h"
#include "Communication/NetComm/NetComm.h"
#include "Communication/Socket/TcpSocket/VmiTcpServerSocket.h"

class NetCommServer;

class ServerNotice : public VmiEventNotice {
public:
    /**
     * @功能描述：ServerNotice构造函数
     */
    explicit ServerNotice(NetCommServer* com);

    ServerNotice() = delete;
    /**
     * @功能描述：ServerNotice虚析构函数
     */
    ~ServerNotice() override;

    /**
     * @功能描述：服务器监听socket异常回调通知，会重启Agent流程
     * @参数 [in] socket：服务端socket
     */
    void Notice(std::shared_ptr<VmiSocket> socket) override;

private:
    NetCommServer* m_server = nullptr;
};

class VmiClientService : public VmiServerService {
public:
    /**
     * @功能描述：VmiClientService构造函数
     */
    explicit VmiClientService(NetCommServer *netComm);

    VmiClientService() = delete;

    /**
     * @功能描述：VmiClientService析构函数
     */
    ~VmiClientService() override;

    /**
     * @功能描述：TCP服务端有新socket连接上时回调函数
     * @参数 [in] socket：新连接的客户端socket
     */
    void Callback(std::shared_ptr<VmiSocket> socket) override;
private:
    NetCommServer* m_server = nullptr;
};

class NetCommServer : public std::enable_shared_from_this<NetCommServer> {
public:
    class EventListener {
    public:
        /**
         * @功能描述：构造函数
         */
        EventListener() = default;
        /**
         * @功能描述：虚析构函数
         */
        virtual ~EventListener() {};
        /**
         * @功能描述：事件通知
         */
        virtual void OnConnect(std::shared_ptr<NetComm> netComm) = 0;
        virtual void OnBreak(std::shared_ptr<NetCommServer> server) = 0;
    };

    /**
     * @功能描述：禁用默认构造函数
     */
    NetCommServer() = delete;
    /**
     * @功能描述：构造函数
     * @参数 [in] port：端口
     * @参数 [in] listener：事件监听器
     */
    NetCommServer(unsigned short port, std::shared_ptr<EventListener> listener);
    /**
     * @功能描述：析构函数，释放对象
     */
    virtual ~NetCommServer();
    /**
     * @功能描述：获取状态
     * @返回值：当前状态（空闲态/运行态）
     */
    int GetStatus();
    /**
     * @功能描述：设置端口
     * @参数 [in] port：端口
     */
    virtual void SetPort(unsigned short port);
    /**
     * @功能描述：设置事件监听器
     */
    void SetEventListener(std::shared_ptr<EventListener> listener);
    /**
     * @功能描述：处理新连接事件
     * @参数 [in] vmisocket：代表新连接的VmiSocket对象
     */
    void HandleConnect(std::shared_ptr<VmiSocket> vmisocket);
    /**
     * @功能描述：处理server socket异常断开
     */
    void HandleBreak();
    /**
     * @功能描述：启动通信监听服务端
     * @返回值：成功返回true，失败返回false
     */
    virtual bool Start();
    /**
     * @功能描述：停止通信监听服务端
     */
    virtual void Stop();

private:

    const int STATUS_IDLE = 0;
    const int STATUS_RUNNING = 1;
    int m_status = 0;
    unsigned short m_port = 0;

    std::shared_ptr<EventListener> m_eventListener = nullptr;
    std::shared_ptr<ServerNotice> m_serverNotice = nullptr;
    std::shared_ptr<VmiClientService> m_clientServer = nullptr;
    VmiTcpServerSocket *m_serverSocket = nullptr;
};
#endif
